let customeraddModule = (function () {
  let $username = $(".username"),
    $usernameSpan = $(".usernameSpan"),
    $male = $("#male"),
    $female = $('#female'),
    // $sexSpan = $(".sexSpan"),
    $useremail = $(".useremail"),
    $useremailSpan = $(".useremailSpan"),
    $userphone = $(".userphone"),
    $userphoneSpan = $(".userphoneSpan"),
    $userqq = $(".userqq"),
    $userqqSpan = $(".userqqSpan"),
    $userweixin = $(".userweixin"),
    $userweixinSpan = $(".userweixinSpan"),
    $selectvalue = $(".selectvalue"),
    $selectvalueSpan = $(".selectvalueSpan"),
    $address = $(".address"),
    $addressSpan = $('.addressSpan'),
    $submit = $('.submit'),
    customerId = null;

  // 绑定
  async function bindCustomerInfo() {
    let result = await axios.get('/customer/info', { params: { customerId } });
    let {
      QQ,
      address,
      email,
      id,
      name,
      phone,
      sex,
      type,
      userId,
      userName,
      weixin,
    } = result.data;
    console.log(result.data)
    if (result.code === 0) {
      $username.val(name);
      sex == 0 ? $male.prop('checked', true) : $female.prop('checked', true);
      $useremail.val(email);
      $username.val(name);
      $userphone.val(phone);
      $userqq.val(QQ);
      $userweixin.val(weixin);
      $selectvalue.val(type);
      $address.val(address);
      // $submit.val(weixin);
      return;
    }
    alert("网络出错，请重试或者联系管理员！");
    customerIds = null;
  }

  // 表单校验：用户名|邮箱|电话|qq
  function checkUserName() {
    let val = $username.val().trim();
    if (val.length === 0) {
      $usernameSpan.html("用户名不能为空！");
      return false;
    }
    // if (!/^[\u4E00-\u9FA5]$/.test(val)) {
    //   $usernameSpan.html("用户名以汉字开头！");
    //   return false;
    // }
    $usernameSpan.html("");
    return true;
  }
  function checkEmail() {
    let val = $useremail.val().trim();
    if (val.length === 0) {
      $useremailSpan.html("邮箱不能为空！");
      return false;
    }
    if (!/^\w+((-\w+)|(\.\w+))*@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/.test(val)) {
      $useremailSpan.html("邮箱格式错误！");
      return false;
    }
    $useremailSpan.html("");
    return true;
  }
  function checkPhone() {
    let val = $userphone.val().trim();
    if (val.length === 0) {
      $userphoneSpan.html("手机号码不能为空！");
      return false;
    }
    // if (!/^1[345789]\d{9}$/.test(val)) {
    //   $userphoneSpan.html("手机号码格式错误！");
    //   return false;
    // }
    $userphoneSpan.html("");
    return true;
  }
  function checkQQ() {
    let val = $userqq.val().trim();
    if (val.length === 0) {
      $userqqSpan.html("qq不能为空！");
      return false;
    }
    if (!/^[1-9]\d{4,14}$/.test(val)) {
      $userqqSpan.html("qq格式错误,为5-15数字！");
      return false;
    }
    $userqqSpan.html("");
    return true;
  }


  // 新增
  async function submitHandle() {
    // 表单数据的二次校验
    if (!checkUserName() || !checkEmail() || !checkPhone() || !checkQQ()) return;

    // 获取用户输入的信息
    // customerId=xxx&name=xxx&sex=xxx&email=xxx&phone=xxx&QQ=xxx&weixin=xxx&type=xxx&address=xxx
    let params = {
      name: $username.val().trim(),
      sex: $male.prop('checked') ? 0 : 1,
      email: $useremail.val().trim(),
      phone: $userphone.val().trim(),
      QQ: $userqq.val().trim(),
      weixin: $userweixin.val().trim(),
      type: $selectvalue.val().trim(),
      address: $address.val().trim(),
    };
    // 向服务器发送请求(区分是新增还是修改)
    if (customerId) {
      // 修改
      params.customerId = customerId;
      let result = await axios.post("/customer/update", params);
      if (result.code == 0) {
        alert('修改成功!');
        window.location.href = '../page/customerlist.html';
        return;
      }
      alert('修改失败，请重新或者联系管理员！');
      return;
    }
    // 新增 
    let result = await axios.post("/customer/add", params);
    if (result.code === 0) {
      alert('添加成功!');
      window.location.href = '../page/customerlist.html';
      return true;
    }
    alert('添加失败，请重新或者联系管理员！');
  }

  return {
    init() {
      // 获取传递参数
      let params = window.location.href.queryURLParams();
      console.log(params)
      if (params.hasOwnProperty("customerId")) {
        customerId = params.customerId;
        bindCustomerInfo();
      }
      // 点击提交按钮
      $submit.click(submitHandle);
    }
  }
})();
customeraddModule.init();